package cz.possoft.serializable.generic;

import java.io.Serializable;

import cz.possoft.converter.jaxb.JAXBGameConverter;

/**
 *
 * @author mara
 * 
 * Predstavuje jednu kteroukoliv hru pro 2 hrace
 */
public abstract class Game implements Serializable {
	
	public static final long serialVersionUID = 734978989;	
	
	private HraciPole hraciPole;
	
    private String whitePlayerName;
    private String blackPlayerName;

    // true pokud je prave na tahu bily
    private boolean whiteNaTahu;

    // true pokud jsem bilej hrac
    private boolean meWhite;
    
    // true pokud hra uz zacala
    private boolean gameBegin;

    // true pokud je hra v aktivnim stavu. Pri neaktivnim nemuze zadny hrac hrat.
    private boolean active;

    // true pokud hra uz byla ukoncena (matem, remizou...)
    private boolean endState;

    public boolean isEndState() {
        return endState;
    }

    public void setEndState(boolean endState) {
        this.endState = endState;                
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public boolean isGameBegin() {
        return gameBegin;
    }

    public void setGameBegin(boolean gameBegin) {
        this.gameBegin = gameBegin;
    }
    
    public String getBlackPlayerName() {
        return blackPlayerName;
    }

    public void setBlackPlayerName(String blackPlayerName) {
        this.blackPlayerName = blackPlayerName;
    }
    
    public boolean isWhiteNaTahu() {
        return whiteNaTahu;
    }

    public boolean isMeWhite() {
        return meWhite;
    }

    public void setMeWhite(boolean m) {
        meWhite = m;
    }

    public void setWhiteNaTahu(boolean whiteNaTahu) {
        this.whiteNaTahu = whiteNaTahu;
    }

    public String getWhitePlayerName() {
        return whitePlayerName;
    }

    public void setWhitePlayerName(String whitePlayerName) {
        this.whitePlayerName = whitePlayerName;
    }

	public HraciPole getHraciPole() {
		return hraciPole;
	}

	public void setHraciPole(HraciPole hraciPole) {
		this.hraciPole = hraciPole;
	}   
	
	// vraci true pokud je na tahu aktivni hrac
	public boolean jsemNaTahu() {
		return ((isMeWhite() && isWhiteNaTahu()) || (!isMeWhite() && !isWhiteNaTahu()));
	}
	
	// vraci moje jmeno
	public String getMyName() {
		if (isMeWhite()) {
			return getWhitePlayerName();
		}
		else {
			return getBlackPlayerName();
		}
	}
	
	// vraci oponentovo jmeno
	public String getOpponentName() {
		if (isMeWhite()) {
			return getBlackPlayerName();
		}
		else {
			return getWhitePlayerName();
		}		
	}
		
	// volano jeste v dobe, kdy je zobrazen panel s hrou, ale hra jeste nezacala a neni nikdo vyzvan
    // pri konstrukci neni hra vubec inicializovana. Sachovnice i jmena hracu zustavaji prazdne
    public Game setNoGame() {
        setGameBegin(false);
        setBlackPlayerName("");
        setWhitePlayerName("");        
        setWhiteNaTahu(false);
        setMeWhite(false);            	
    	return this;
    }    
    
    // volano ihned po inicializaci hry.
    public Game setNewGameState(String whitePlayerName, String blackPlayerName, boolean iAmWhite) {
        setGameBegin(true);
        setBlackPlayerName(blackPlayerName);
        setWhitePlayerName(whitePlayerName);               
        setWhiteNaTahu(true);               
        setMeWhite(iAmWhite);    	
    	return this;    	    	
    }
    
    /** default implementation returns null, but should be overriden in all games, which will be saved in JAXB format. */
    public JAXBGameConverter<? extends Game> getJAXBGameConverter() {
    	throw new UnsupportedOperationException("Method getJAXBGameConverter must be overriden in class " + getClass().getName());
    }
    
    /* returns key of the game */
    public String getGameKey() {
    	String key = this.getClass().getSimpleName();
    	int index = key.indexOf("Game");
    	return key.substring(0, index);
    }

}
